package com.mvc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mvc.pojo.Department;
import com.mvc.pojo.Employee;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface EmployeeMapper extends BaseMapper<Employee> {

    @Update("<script>        " +
            "update employee\n" +
            "        <set>\n" +
            "            <if test=\"realname != null\">\n" +
            "                realname = #{realname},\n" +
            "            </if>\n" +
            "            <if test=\"password != null\">\n" +
            "                password = #{password},\n" +
            "            </if>\n" +
            "            <if test=\"cellphone != null\">\n" +
            "                cellphone = #{cellphone},\n" +
            "            </if>\n" +
            "            <if test=\"type != null\">\n" +
            "                type = #{type},\n" +
            "            </if>\n" +
            "            <if test=\"deptId != null\">\n" +
            "                dept_id = #{deptId},\n" +
            "            </if>\n" +
            "        </set>\n" +
            "        where id = #{id}" +
            "</script>")
    void updateEmployee(Employee employee);

//    @Select("select * from employee")
    IPage<Employee> selectEmployeePage(IPage<Employee> page);

    @Results(id = "empMap",value = {
            @Result(property = "id",column = "id"),
            @Result(property = "realname",column = "realname"),
            @Result(property = "cellphone",column = "cellphone"),
            @Result(property = "type",column = "type"),
            @Result(property = "deptId",column = "dept_id"),
            @Result(property = "department",column = "dept_id",
                    javaType = Department.class,
                    one = @One(select = "com.mvc.mapper.DepartmentMapper.selectById"))
    })
    @Select("<script>" +
            "select * from employee " +
            "<where>\n" +
            "            <if test=\"realname != null and realname != ''\">\n" +
            "                and realname like concat('%',#{realname},'%')\n" +
            "            </if>\n" +
            "</where>" +
            "</script>")
    List<Employee> selectEmployees(Employee employee);

    List<Employee> selectEmployeesByIds(@Param("ids") List<Long> ids);

    @Select("select * from employee where dept_id = #{deptId}")
    List<Employee> selectByDeptId(Long deptId);
}
